package uit.shoeshop.lib;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


public class Util {
	public static Connection Connect(){
		Connection conn = null;
		try{            
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
			String url = "jdbc:mysql://localhost:3306/"+ Constants.DB_NAME +"?useUnicode=true&characterEncoding=UTF-8";
			conn = DriverManager.getConnection(url, "root", "");
		}catch (Exception e)
		{
			conn=null;
		}
		
		return conn;
	}
	public static String Panigation(String url,int total,int limit,int cur_page){
		StringBuilder html = new StringBuilder();
		html.append("");
		int total_page = (int)Math.floor(total/limit);
        if (total % limit != 0) total_page++;
        if (total_page > 1)
        {
        	int n = 0;
        	html.append("<div class='pagination'><p id='paging' align='center'>");
        	html.append("<span><a href='" + url + "1'><b><< Đầu</b></a></span>");
            if (cur_page < 4){
            	n = ((cur_page - 1 < 1) ? 1 :cur_page - 1);
            	html.append(" <span><a href='" + url + n + "'></span></a>");
            }else{
            	n = ((cur_page - 10 < 1) ? 1 : cur_page - 10);
            	html.append(" <a href='" + url + n + "'></a>");
            	html.append("…");
            }
			
            if (cur_page > 3 && cur_page < total_page - 2)
            {
            	n = cur_page + 2;
                for (int i = cur_page - 2; i <= n; i++)
                {
                    if (cur_page == i)
                    	html.append(" <span style='font-size:200%;font-weight:bold;' class='currentpage'><span>" + i + "</span></span>");
                    else
                    	html.append(" <a href='"+ url + i  + "'>"+ i + "</a>");
                }
            }
            else if (cur_page <= 3)
            {
            	n = (total_page < 5 ? total_page : 5);
                for (int i = 1; i <= n; i++)
                {
                	
                    if (cur_page == i)
                    	html.append(" <span style='font-size:200%;font-weight:bold;' class='currentpage'><span>"+ i + "</span></span>");
                    else
                    	html.append(" <a href='"+ url + i  + "' >" + i + "</a>");
                }
            }
            else
            {
            	n= (total_page < 5 ? 3 : 4);
                for (int i = total_page - n; i <= total_page; i++)
                {
                    if (cur_page == i)
                    	html.append(" <span style='font-size:200%;font-weight:bold;' class='currentpage'><span>"+ i + "</span></span>");
                    else
                    	html.append(" <a href='"+ url + i  + "' >"+ i + "</a>");
                }
            }
			
            if (cur_page > total_page - 3)
            	html.append(" <span class='disabled'>&nbsp;</span>");
            else
            {
            	n = ((cur_page + 10 > total_page) ? total_page : cur_page + 10);
            	html.append(" …");
            	html.append(" <a href='" + url  + n + "'></a>");
            }
            html.append(" <span><a href='"+url + total_page +"' ><b>Cuối >></b></a></span>");
            html.append("</p></div>");
        }		
		return html.toString();
	}
    public static boolean isIntNumber(String num){
        try{
        	Integer.parseInt(num);
        } catch(NumberFormatException nfe) {
        	return false;
        }
        return true;
   }
    
    public static Object getSessions(String name,HttpServletRequest request){
    	HttpSession sessions = request.getSession();
    	Object value = sessions.getAttribute(name);
    	return value;
    }

    public static void addSessions(String name,HashMap<Integer,Integer> value,HttpServletRequest request){		
		if(!name.equals("")){
			HttpSession session = request.getSession(true);
			session.setAttribute(name, value);	
		}    	
    }
    
    public static void deleteSessions(String name,HttpServletRequest request){
    	HttpSession sessions = request.getSession();
    	sessions.removeAttribute(name);
    }
    
    public static Integer getLogin(String name,HttpServletRequest request){
    	HttpSession sessions = request.getSession();
    	Object value = sessions.getAttribute(name);
    	if(value == null) value = 0;
    	return (Integer)value;
    }

    public static void setLogin(String name,Integer value,HttpServletRequest request){		
		if(!name.equals("")){
			HttpSession session = request.getSession(true);
			session.setAttribute(name, value);	
		}    	
    }
    	
    public static String del_space(String str){
    	String result = "";
		str = str.trim();str.toCharArray();
		char[] character = str.toCharArray();
		for (int i = 0;i < character.length;i++)
		{
			if(character[i]!=' ')
				result+= character[i];
			if(character[i]==' ' && character[i+1]!=' ')
				result+= character[i];
		}

		return result;
	}
    
    public static String MD5(String str){
    	MessageDigest md;
        StringBuffer sb = new StringBuffer();
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
	        byte byteData[] = md.digest();
	        //convert the byte to hex format method 1
	        for (int i = 0; i < byteData.length; i++) {
	         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	        }
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sb.toString();
	}
}
